﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SCM.Model.DBEntity;
using SCM.Servie.IBusiness;
using System.Linq;

namespace SCM.Manage.Controllers
{
    [Authorize]
    public class BasicCompanyController : BaseController
    {
        public IActionResult Index()
        {
            return View();
        }

        #region 查询
        [HttpPost]
        public IActionResult Search(string companyName,string shortName,string identityNumber,string telPhone, int index = 1)
        {
            var service = GetService<ICompanyService>();
            var userPage = service.Query(companyName, shortName,identityNumber,telPhone, new Common.PageInfo() { PageIndex = index, PageSize = 10 });
            return GetOk(new { totalCount = userPage.Page.DataCount, items = userPage.Data });
        }

        #endregion

        #region 编辑
        public IActionResult EditView(string id)
        {
            var model = GetService<ICompanyService>().GetById(id);
            return View(model);
        }

        public IActionResult Modify(T_Company entity)
        {
            entity.ModifyUserId = CurrentUserId;
            entity.ModifyUserName = CurrentUserName;

            bool result = GetService<ICompanyService>().UpdateValidDate(entity);
            return result ? GetOk(result) : Error("保存失败");
        }
        #endregion


        #region 删除
        /// <summary>
        /// 删除  【支持批量操作】
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public IActionResult Delete(string ids)
        {
            if (string.IsNullOrWhiteSpace(ids) || ids.Split(',').Length < 0)
            {
                return Error("请选择数据");
            }
            var delIds = ids.Split(',').ToList();
            bool result = GetService<ICompanyService>().Delete(delIds);
            return result ? GetOk(result) : Error("删除失败");
        }
        #endregion
    }
}
